package com.sinog.biz.service.common.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sinog.biz.mapper.havexml.DepartmentMapper;
import com.sinog.biz.mapper.havexml.SystemCodeMapper;
import com.sinog.biz.service.common.CommonService;
import com.sinog.biz.util.CodeUrlUtils;
import com.sinog.core.domain.Department;
import com.sinog.core.domain.SystemCode;
import com.sinog.core.model.UserDTO;
import com.sinog.core.utilbean.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @desc 系统公用的service
 * @author lzq
 * @date 2020-02-17
 */
@Slf4j
@Service
public class CommonServiceImpl implements CommonService {

    @Autowired
    private SystemCodeMapper systemCodeMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 获取下拉框数据
     * @param codeType codeType
     * @return List<Map < String,Object>>
     */
    @Override
    public List<Map<String,Object>> getComboboxData(String codeType) {
        List<SystemCode> systemCodes;
        // 职业技能用到的下拉
        if(StringUtils.equals(codeType,Constants.CodeType.CODE_XLJY)) {
            return systemCodeMapper.getCritypeCodeP(codeType);
        } else {
            systemCodes = systemCodeMapper.getComboboxData(codeType);
        }
        List<Map<String,Object>> list = new ArrayList<>(16);
        for(SystemCode sc : systemCodes) {
            Map<String,Object> map = new HashMap<>(16);
            map.put("codeId",sc.getCodeId());
            map.put("codeName",sc.getCodeName());
            list.add(map);
        }
        return list;
    }

    /**
     * 获取省市县信息
     * @param codeType 行政区划的code_type默认为2_1
     * @param codeId 选中的code编码
     * @param flag flag = 1，代表只查询本系统的省份
     * @return List<Map < String,Object>>
     */
    @Override
    public List<Map<String,Object>> getCodeDetail(String codeType,String codeId,String flag) {
        //获取本地code
        String localCode = CodeUrlUtils.getLocalCode();
        Map<String,Object> cloumMap = new HashMap<>(16);
        cloumMap.put("code_type",codeType);
        cloumMap.put("pcode_id",codeId);
        List<SystemCode> systemCodes = systemCodeMapper.selectByMap(cloumMap);
        List<Map<String,Object>> list = new ArrayList<>(16);
        for(SystemCode sc : systemCodes) {
            Map<String,Object> map = new HashMap<>(16);
            map.put("codeId",sc.getCodeId());
            map.put("codeName",sc.getCodeName());
            if(StringUtils.equals(Constants.NumberStr.NUM_STR_FU_ONE,codeId)) {
                // flag.equals("1") 代表只查询本省区划
                if(!StringUtils.equals(sc.getCodeId(),localCode) && !Constants.NumberStr.NUM_STR_ONE.equals(flag)) {
                    list.add(map);
                } else if(StringUtils.equals(sc.getCodeId(),localCode)) {
                    list.add(0,map);
                }
            } else {
                list.add(map);
            }
        }
        return list;
    }

    /**
     * 获取部门
     * @param id 当前部门编号
     * @return Department
     */
    @Override
    public Department getDepartmentById(String id) {
        return departmentMapper.getDepartmentById(id);
    }

    /**
     * 获取下拉框数据
     * @param departid departid
     * @return List<Map < String,String>>
     */
    @Override
    public List<Map<String,String>> getJyData(String departid) {
        return departmentMapper.getJyData(departid);
    }

    /**
     * 查询tbsys_code编码
     * @param codeType 编码类型
     * @param pcodeId 父级编码  0 不启用  99表示不等于-1
     * @param sn 是否排序：-1 不排序 0 desc 1 asc
     * @param userDTO userDTO
     * @return List<Map<String,Object>>
     */
    @Override
    public List<Map<String,Object>> getTbsysCode(String codeType, String pcodeId, String sn, UserDTO userDTO) {
        List<Map<String,Object>> tbsysCodeList = systemCodeMapper.getTbsysCode(codeType,pcodeId,sn);
        //拿到当前用户的角色id,A11为司法所
        String level = userDTO.getLevel();
        //如果是看守所用户,展示拘役不展示管制，所以遍历去删除它
        if(Constants.NumberStr.NUM_STR_ZERO_TWO.equals(level) && !tbsysCodeList.isEmpty()) {
            tbsysCodeList.removeIf(map -> Constants.NumberStr.NUM_STR_NINE_NINE_NINE_FOUR.equals(map.get("codeId")));
        }
        //如果是监狱用户,不展示拘役不展示管制，所以遍历去删除它
        if(Constants.NumberStr.NUM_STR_ZERO_ONE.equals(level) && !tbsysCodeList.isEmpty()) {
            Iterator<Map<String,Object>> iterator = tbsysCodeList.iterator();
            while(iterator.hasNext()) {
                Map<String,Object> map = iterator.next();
                if(Constants.NumberStr.NUM_STR_NINE_NINE_NINE_FOUR.equals(map.get("codeId"))) {
                    iterator.remove();
                }
                if(Constants.NumberStr.NUM_STR_NINE_NINE_NINE_EIGHT.equals(map.get("codeId"))) {
                    iterator.remove();
                }
            }
        }
        return tbsysCodeList;
    }

    /**
     * 根据code_id和code_type查询tbsys_code的code_name
     * @param code code
     * @return String
     */
    @Override
    public String getCodeNameByCode(String code) {
        Map<String,String> map = new HashMap<>(16);
        map.put("codeType", Constants.CodeType.CODE_ZHRMGHG_XZQH_DM);
        map.put("codeId", code);
        return systemCodeMapper.getCodeByMap(map).getCodeName();
    }

    /**
     * 根据CodeName获取字典信息
     * @param mapCode mapCode
     * @param name 地区名
     * @return SystemCode
     */
    @Override
    public SystemCode getTbsysCodeByCodeName(String mapCode, String name) {
        List<SystemCode> tbsysCodes = systemCodeMapper.getTbsysCodeByCodeName(mapCode, name);
        if(0 >= tbsysCodes.size()) {
            String substring = mapCode.substring(0, 2);
            tbsysCodes = systemCodeMapper.getTbsysCodeByPcodeId(substring, name);
        }
        return tbsysCodes.get(0);
    }

    /**
     * 获取mapcode下的所有子地区的code
     * @param mapCode 区划编码
     * @return List<String>
     */
    @Override
    public List<String> getChildCodesByPcode(String mapCode) {
        return systemCodeMapper.getChildCodesByPcode(mapCode);
    }

    /**
     * 获取省市县
     * @param codeType codeType
     * @param codeId codeId
     * @return List<Map < String,Object>>
     */
    @Override
    public List<Map<String,Object>> getDepart(String codeType,String codeId) {
        //获取本地code
        String localCode = CodeUrlUtils.getLocalCode();
        Map<String,Object> cloumMap = new HashMap<>(16);
        cloumMap.put("code_type",codeType);
        cloumMap.put("pcode_id",codeId);
        List<SystemCode> systemCodes = systemCodeMapper.selectByMap(cloumMap);
        List<Map<String,Object>> list = new ArrayList<>(16);
        for(SystemCode sc : systemCodes) {
            Map<String,Object> map = new HashMap<>(16);
            map.put("codeId",sc.getCodeId());
            map.put("codeName",sc.getCodeName());
            if(StringUtils.equals("-1",codeId)) {
                if(!StringUtils.equals(sc.getCodeId(),localCode)) {
                    list.add(map);
                } else {
                    list.add(0,map);
                }
            } else {
                list.add(map);
            }
        }
        return list;
    }

    /**
     * 获取省街道
     * @param parentCode parentCode
     * @return List<Map < String,Object>>
     */
    @Override
    public List<Map<String,Object>> getStreet(String parentCode) {
        Map<String,Object> map = new HashMap<>(16);
        map.put("pcode_id",parentCode);
        map.put("code_type", Constants.CodeType.CODE_ZHRMGHG_XZQH_DM);
        List<SystemCode> list = systemCodeMapper.selectByMap(map);
        List<Map<String,Object>> arrList = new ArrayList<>(16);
        for(SystemCode l : list) {
            Map<String,Object> m = new HashMap<>(16);
            m.put("code",l.getCodeId());
            m.put("name",l.getCodeName());
            arrList.add(m);
        }
        return arrList;
    }

    /**
     * 描述
     * @return List<SystemCode>
     */
    @Override
    public List<SystemCode> getAllProvince() {
        return systemCodeMapper.selectList(new QueryWrapper<SystemCode>().eq("pcode_id","-1").eq("code_type",Constants.CodeType.CODE_ZHRMGHG_XZQH_DM));
    }

    /**
     * 根据角色查询帮扶范围
     * @param roleid 角色
     * @return List<Map<String,Object>>
     */
    @Override
    public List<Map<String,Object>> getBffw(String roleid, @RequestParam("flog") String flog) {
        List<SystemCode> systemCodes = new ArrayList<>(16);
        if(Constants.NumberStr.NUM_STR_ONE.equals(flog)){
            systemCodes = systemCodeMapper.getComboboxData(Constants.CodeType.CODE_BFFW);
        }else {
            switch(roleid){
                case Constants.RoleId.ROLE_ID_SFT:
                    systemCodes = systemCodeMapper.getComboboxData(Constants.CodeType.CODE_BFFW_SFT);
                    break;
                case Constants.RoleId.ROLE_ID_DSSFJ:
                    systemCodes = systemCodeMapper.getComboboxData(Constants.CodeType.CODE_BFFW_DSSFJ);
                    break;
                case Constants.RoleId.ROLE_ID_QXSFJ:
                    systemCodes = systemCodeMapper.getComboboxData(Constants.CodeType.CODE_BFFW_QXSFJ);
                    break;
                case Constants.RoleId.ROLE_ID_SFS:
                    systemCodes = systemCodeMapper.getComboboxData(Constants.CodeType.CODE_BFFW_SFS);
                    break;
            }
        }
        List<Map<String,Object>> list = new ArrayList<>(16);
        for(SystemCode sc : systemCodes) {
            Map<String,Object> map = new HashMap<>(16);
            map.put("codeId",sc.getCodeId());
            map.put("codeName",sc.getCodeName());
            list.add(map);
        }
        return list;
    }


    /**
     * 获取省市县锁定信息
     * @return Map<String,Object>
     */
    @Override
    public Map<String,Object> getAddressLocking(UserDTO userDTO) {
        String codeId;
        if(Constants.OrgLevel.LEVEL_SFS.equals(userDTO.getLevel())){
            codeId = "jiedao_code";
        }else if(Constants.OrgLevel.LEVEL_QXSFJ.equals(userDTO.getLevel())){
            codeId = "quxian_code";
        }else if(Constants.OrgLevel.LEVEL_DSSFJ.equals(userDTO.getLevel())){
            codeId = "dishi_code";
        }else {
            codeId = "sheng_code";
        }
        Map<String,Object> map = systemCodeMapper.getAddressLocking(codeId, userDTO.getDepartid());
        map.put("level", userDTO.getLevel());
        return map;
    }

    /**
     * 更新行政区划四层拼接字符串
     * 更新用户部门缓存
     */
    public void executeDepartChild() {

    }
}